package com.sprouting.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsSite;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsSiteQo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsSiteService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 现场
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/11/8 14:45
 */
@Slf4j
@RestController
@RequestMapping("opsSite")
public class OpsSiteController {

    @Resource
    private OpsSiteService opsSiteService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;




    /**
     * 根据主键查询场所信息
     * @param siteId 场所信息
     * @return
     */
    @RequestMapping("getSiteById")
    public ResultJson getSiteById(String siteId){
        if (CheckTool.checkNull(siteId)){
            return ResultJson.errorCheck();
        }

        return ResultJson.success(opsSiteService.getSiteById(siteId));
    }

    /**
     * 逻辑删除现场
     * @param siteId 现场id
     * @return
     */
    @IdempotentSingle
    @PostMapping("delOpsSite")
    public ResultJson delSite(String siteId){
        if (CheckTool.checkNull(siteId)){
            return ResultJson.errorCheck();
        }

        OpsSite site = opsSiteService.getById(siteId);
        if (site == null){
            return ResultJson.success();
        }

        DromaraFileStorage opsUploadFile = dromaraFileStorageService.delFileBefore(site.getIco());
        // 逻辑删除
        opsSiteService.delSite(site, opsUploadFile);
        return ResultJson.success();
    }

    /**
     * 分页查询现场
     * @param query 查询参数
     * @return
     */
    @RequestMapping("getSitePageList")
    public ResultJson getSitePageList(OpsSiteQo query){
        Page page = query.getPage();
        return ResultJson.success(opsSiteService.getSitePageList(page, query));
    }

    /**
     * 保存一个现场
     * @param opsSite 现场
     * @return
     */
    @IdempotentSingle
    @PostMapping("saveSite")
    public ResultJson saveSite(@Validated(AddGroup.class) OpsSite opsSite){
        String msg = checkSite(opsSite);
        if (msg != null){
            return ResultJson.error(msg);
        }

        opsSite.setId(IdTool.getId());
        EntityTool.saveBase(opsSite);

        // 处理图片
        DromaraFileStorage opsUploadFile = dromaraFileStorageService.toProductionBefore(opsSite.getIco(), opsSite.getId(), ConstantBase.FileSourceType.SITE_ICO);

        opsSiteService.saveSite(opsSite, opsUploadFile);
        return ResultJson.success(opsSite);
    }

    /**
     * 更新现场
     * @param opsSite 现场
     * @return
     */
    @IdempotentSingle
    @PostMapping("updSite")
    public ResultJson updSite(@Validated(UpdGroup.class) OpsSite opsSite){
        String msg = checkSite(opsSite);
        if (msg != null){
            return ResultJson.error(msg);
        }

        EntityTool.updBase(opsSite);

        // 处理图片
        OpsSite oldDb = opsSiteService.getById(opsSite.getId());
        if (oldDb == null){
            return ResultJson.errorCheck();
        }
        DromaraFileStorage[] uploadFileArray = dromaraFileStorageService.updFileBefore(opsSite.getIco(), oldDb.getIco(), opsSite.getId(), ConstantBase.FileSourceType.SITE_ICO);

        opsSiteService.updSite(opsSite, uploadFileArray);
        return ResultJson.success(opsSite);
    }

    /**
     * 检查现场
     * @param opsSite 现场数据
     * @return 非null则有异常
     */
    private String checkSite(OpsSite opsSite){
        OpsSite dbName = opsSiteService.getByNameFilterId(opsSite.getName(), opsSite.getId());
        if (dbName != null){
            return "现场名称已使用，请勿重复注册";
        }
        if (opsSite.getImportance() == null){
            opsSite.setImportance(3);
        }
        if (opsSite.getSecrecy() == null){
            opsSite.setSecrecy(ConstantBase.YesOrNo.NO);
        }
        if (opsSite.getStage() == null){
            opsSite.setStage(ConstantBase.SiteStage.INIT);
        }

        return null;
    }

}
